/**
 * Project: apollo-base-tools
 * 
 * File Created at 2017年7月31日
 * 
 * Copyright 2015-2016 dx.com Croporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * DongXue software Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with dx.com.
 */
package org.apollo.base.tools.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowStateListener;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.SwingUtilities;
import javax.swing.ToolTipManager;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;

import org.apollo.base.tools.ui.constants.UIConstants;
import org.apollo.base.tools.ui.view.JAccordionPanel;
import org.apollo.base.tools.ui.view.JDynamicTabbedPane;
import org.apollo.swing.component.JImagePane;
import org.apollo.swing.componentc.JCFrame;
import org.apollo.swing.componentc.JCLabel;
import org.apollo.swing.layout.LineLayout;
import org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper;

/**
 * @ClassName: MainFrame
 * @Description: mytools入口
 * @author wuzhenfang(wzfbj2008@163.com)
 * @date 2017年7月31日 上午9:26:01
 * @version V1.0
 */
public class MainFrame extends JCFrame {

	private static final long serialVersionUID = 5579283325476879977L;

	private JComponent content;
	private JImagePane titleContent, mainPane,statusPanel;
	private JPanel buttonPane;
	
	private JPanel topPanel;// 头
	private JSplitPane workSplitPane;// 分左右两个区域
	private JAccordionPanel accordionPanel;// 左侧菜单
	private JDynamicTabbedPane dynamicTabbedPane;// 工作页签
	
	private MenuManager manager;// 菜单管理器

	/**
	 * Create the frame.
	 */
	public MainFrame() {
		super();
		initFrame();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.setProperty("sun.java2d.noddraw", "true");
		ToolTipManager.sharedInstance().setInitialDelay(200);
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				try {
					org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper.launchBeautyEyeLNF();
					UIManager.put("RootPane.setupButtonVisible", false);
					// 设置本属性将改变窗口边框样式定义
					BeautyEyeLNFHelper.frameBorderStyle = BeautyEyeLNFHelper.FrameBorderStyle.osLookAndFeelDecorated;
					org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper.launchBeautyEyeLNF();
					// 调整默认字体
					for (int i = 0; i < UIConstants.DEFAULT_FONT.length; i++) {
						UIManager.put(UIConstants.DEFAULT_FONT[i], new Font("微软雅黑", Font.PLAIN, 13));
					}
					
					String kk = UIManager.getSystemLookAndFeelClassName();// 获得当前系统样式名称字符串。
					UIManager.setLookAndFeel(kk);// 加载样式。
					
					// 初始化frme界面
					MainFrame mainFrame = new MainFrame();
					mainFrame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	private void initFrame() {
		setTitle("江北青衣-工具箱");
		
		content = (JComponent)getContentPane();
		content.setLayout(new BorderLayout());
		content.setBorder(new EmptyBorder(0, 2, 5, 2));
        content.setBackground(Color.WHITE);
		
        setIconImage(UIConstants.IMAGE_other);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(UIConstants.height, UIConstants.width);
		setResizable(true);
		setLocationRelativeTo(null);

		// location
		Dimension screSize = Toolkit.getDefaultToolkit().getScreenSize();
		int x = screSize.width;
		int y = screSize.height;
		int appW = (int) getSize().getWidth();
		int appH = (int) getSize().getHeight();
		// display center
		setLocation((x - appW) / 2, (y - appH) / 2);

		// 添加状态最大化最小化事件
		addWindowStateListener(new WindowStateListener() {
			@Override
			public void windowStateChanged(WindowEvent state) {
				if (state.getNewState() == 1 || state.getNewState() == 7) {
				} else if (state.getNewState() == 0) {
				} else if (state.getNewState() == 6) {
				}
			}
		});

		// 添加组件大小监控事件
		addComponentListener(new ComponentListener() {
			@Override
			public void componentResized(ComponentEvent event) {
				String.valueOf(event.getComponent().getSize().getWidth());//实时计算高度和宽度
				String.valueOf(event.getComponent().getSize().getHeight());//实时计算高度和宽度
			}
			@Override
			public void componentMoved(ComponentEvent e) {
			}
			@Override
			public void componentShown(ComponentEvent e) {
			}
			@Override
			public void componentHidden(ComponentEvent e) {
			}
		});
		add(getMainPane(), BorderLayout.CENTER);
		getManager().init();// 初始化菜单和面板管理器
	}

	public JImagePane getMainPane() {
		if(mainPane == null) {
			mainPane = new JImagePane();
			mainPane.setImageOnly(false);
	        mainPane.setBackground(new Color(255, 255, 255, 230));
	        mainPane.setLayout(new BorderLayout());
			
	        mainPane.add(getButtonPanel(), BorderLayout.NORTH);
	        mainPane.add(getWorkSplitPane(), BorderLayout.CENTER);
	        mainPane.add(getStatusPanel(), BorderLayout.SOUTH);
		}
		return mainPane;
	} 
	
	/**
	 * 头部菜单
	 * @return
	 */
	public JPanel getTopPanel() {
		if (topPanel == null) {
			topPanel = new JPanel();
		}
		return topPanel;
	}
	
	
	public JPanel getButtonPanel() {
		if(buttonPane == null) {
			buttonPane = new JImagePane(UIConstants.IMAGE_TOP_BG, JImagePane.SCALED);
			buttonPane.setPreferredSize(new Dimension(-1, 78));
			buttonPane.setBorder(new EmptyBorder(0, 5, 0, 0));
			buttonPane.setOpaque(false);
			buttonPane.setLayout(new LineLayout(0, 0, 0, 0, 0, LineLayout.LEADING, LineLayout.LEADING, LineLayout.HORIZONTAL));
		}
		return buttonPane;
	}

	public JSplitPane getWorkSplitPane() {
		if (workSplitPane == null) {
			workSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
			workSplitPane.setOneTouchExpandable(false);// 是否让分割线显示出箭头
			workSplitPane.setDividerSize(5);// 分割线的宽度
			workSplitPane.setDividerLocation(0.2);// 设定分割线的距离左边的位置
			workSplitPane.setLeftComponent(getAccordionPanel());
			workSplitPane.setRightComponent(getDynamicTabbedPane());
		}
		return workSplitPane;
	}

	private JAccordionPanel getAccordionPanel() {
		if (accordionPanel == null) {
			accordionPanel = new JAccordionPanel();
			accordionPanel.setLayout(new BorderLayout());
		}
		return accordionPanel;
	}

	private JDynamicTabbedPane getDynamicTabbedPane() {
		if (dynamicTabbedPane == null) {
			dynamicTabbedPane = new JDynamicTabbedPane();
		}
		return dynamicTabbedPane;
	}
	
	/**
	 * 菜单管理器
	 * @return
	 */
	private MenuManager getManager() {
		if (manager == null) {
			manager = new MenuManager();
			getManager().setAccordionPanel(getAccordionPanel());
			getManager().setTabbedPane(getDynamicTabbedPane());
		}
		return manager;
	}
	
	/**
	 * 底部状态panel
	 * @return
	 */
	private JComponent getStatusPanel() {
		if(statusPanel == null) {
			statusPanel = new JImagePane();
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			JCLabel lbDate = new JCLabel(format.format(new Date()));
			JCLabel lbStatusInfo = new JCLabel("作者：" + "江北青衣" + "  ");
			JCLabel lbTotal = new JCLabel("出品");
			JCLabel lbAmount = new JCLabel(" 冬雪工作室 ");
			statusPanel.setImageOnly(true);
			statusPanel.setPreferredSize(new Dimension(-1, 21));
			statusPanel.setBorder(new EmptyBorder(4, 5, 3, 5));
			statusPanel.setLayout(new LineLayout(8, 0, 0, 0, 0, LineLayout.LEADING, LineLayout.LEADING, LineLayout.HORIZONTAL));
	        lbAmount.setBorder(new EmptyBorder(0, 5, 0, 5));
	        statusPanel.add(lbTotal, LineLayout.END_FILL);
	        statusPanel.add(lbAmount, LineLayout.END_FILL);
	        statusPanel.add(lbDate, LineLayout.END_FILL);
	        statusPanel.add(lbStatusInfo, LineLayout.MIDDLE_FILL);
		}
		return statusPanel;
	}
}
